<?php
class device extends project{

	private $table_checkin = 'syt_checkin'; //设备登记表
	private $table_device = 'syt_device';   //设备表
	private $table_bind = 'syt_bind';   //设备与项目绑定表
	private $table_project = 'syt_project';   //项目表
	private $table_set = 'syt_set';   //渣车参数设置表



	public function del_checkin($data){
		if(!$data){
			return false;
		}
		$sql = "DELETE FROM `{$this->table_checkin}` WHERE (`pid`={$data['pid']} and `time`<{$data['times']})";
		return $this->_mysql(0)->update($sql);
	}


	/**
	 * 添加或者修改设备
	 * @author xiezhihui 2014-6-10 09:29:29
	 * @param mixed $data
	 * @return mixed
	 */
	public function checkin_up_data($data){
		if(!$data) return false;
		return !$data['id']?$this->set_db($this->table_checkin,$data):$this->set_db($this->table_checkin,$data,false,"id={$data['id']}");
	}


	/**
	* 添加或者修改设备
	* @author xiezhihui 2014-6-10 09:29:29
	* @param mixed $data
	* @return mixed
	*/
	public function ins_up_data($data){
		if(!$data) return false;
		return !$data['id']?$this->set_db($this->table_device,$data):$this->set_db($this->table_device,$data,false,"id={$data['id']}");
	}


	/**
	* 新增一个登记信息
	* @author zhufu 2014-06-06
	*
	* @param int $project_id    所属的项目ID
	* @param int $device_id     登记的设备ID
	* @param float $coord_x     登记的坐标经度
	* @param float $coord_y     登记的坐标纬度
	* @param string $img        传回的照片
	* @param int $ctime         登记的实时时间，设备上的时间，可能会和服务器的时间有差别
	* @param int $app_img       客户端的图片名称用于防止重复提交
	* @param int $truck         渣车类型
	* @param int $dust          土石类型
	* @param int $onload        渣车装载类型  1-空车  2-重车
	* @param int $xid           项目负责人id
	* @param strong $dname      设备名称
	* @param strong $downer      机车拥有者
	* @param string $doper		机车操作者
	* @param string $no         设备号
	* @return boolean 成功返回true, 失败返回 false
	*/
	public function add_checkin($project_id, $device_id, $coord_x, $coord_y, $img, $ctime,$type,$app_img,$truck=0,$dust=0,$dname='',$downer='',$doper='',$no='',$onload=0,$xid=0,$group=0,$code='',$wj_no='',$line='',$posi=0,$wj_xh='',$line_dis='',$car_type='',$extend,$gcat=0){
		//参数基本格式判断
		if(!is_numeric($project_id) || !is_numeric($device_id) || !$coord_x || !$coord_y || !$img || !$ctime || !$app_img){
			return false;
		}
		$data = array(
			'pid' => $project_id,
			'did' => $device_id,
			'coord_x' => $coord_x,
			'coord_y' => $coord_y,
			'img' => $img,
			'ctime' => $ctime,
			'time' => NOW,
			'type' => $type,
			'unique_img' => $app_img,
			'truck'=>$truck,
			'dust'=>$dust,
			'onload'=>$onload,
			'dname'=>$dname,
			'downer'=>$downer,
			'doper'=>$doper,
			'dno'=>$no,
			'xid'=>$xid,
			'group'=>$group,
			'code'=>$code,
			'wj_no'=>$wj_no,
			'line'=>$line,
			'posi'=>$posi,
			'line_dis'=>$line_dis,
			'wj_xh'=>$wj_xh,
			'car_type'=>$car_type,
			'gcat'=>$gcat
		);

		$data=array_merge($data,$extend);
		return $this->set_db($this->table_checkin, $data);
	}

	//修改记录信息
	public function update_checkin($id,$data){
		$where="id=".$id;
		return $this->set_db($this->table_checkin, $data,false,$where);
	}

	public function getCarset($uid,$type,$code,$cols='code,comment'){
		$sql="SELECT {$cols} FROM {$this->table_set} WHERE uid={$uid} AND type={$type} AND code='{$code}'";
		return $this->_mysql(0)->findone($sql);
	}


	/**
	 * 获取计时记录是否在同一次的时段内
	 * @param int $pid    项目编号
	 * @param int $did    设备编号
	 * @param int $ctime  当前记录时间
	 * @return number|unknown
	 */
	public function getlast($pid,$did,$ctime){
		$sql = "SELECT `group`,ctime,time FROM {$this->table_checkin} WHERE type=2 AND pid={$pid} AND did={$did} ORDER BY id DESC" ;
		$res=$this->_mysql(0)->findone($sql);
		if(empty($res))return 0;

		//错序上传的数据查找对应的分组 @author leiqianyong 2020-3-26 16:51 PM
		if($ctime<$res['ctime']){
			$sql = "SELECT `group`,ctime,time FROM {$this->table_checkin} WHERE type=2 AND pid={$pid} AND did={$did} AND ctime>{$ctime} ORDER BY ctime ASC" ;
			$res=$this->_mysql(0)->findone($sql);
			if($res['ctime']-$ctime<=300){
				return $res['group'];
			}

			return $res['group']-1;
		}


		//在5min钟内认为是同一次
		if(($ctime-$res['ctime'])<=300){
			return $res['group'];
		}

		return $res['group']+1;
	}


	/**
	* 根据设备编号得到设备信息
	* @author zhufu 2014-06-06
	*
	* @param string $no 设备号
	*/
	public function device_info_by_no($no){
		if(!$no){
			return false;
		}
		$sql = "SELECT * FROM {$this->table_device} WHERE no='{$no}'";
		return $this->_mysql(0)->findone($sql);
	}
	/**
	* 根据app图片查询是否在登记表出现
	*
	* @param mixed $app_img
	* @param int $did
	* @return mysql对象
	*/
	public function device_unique_img($app_img,$did){
		if(!$app_img){
			return false;
		}
		$sql = "SELECT id FROM {$this->table_checkin} WHERE unique_img={$app_img} AND did={$did} order by id desc limit 1 ";
		return $this->_mysql(0)->findone($sql);
	}
	/**
	* 得到一个设备的绑定信息
	* @author zhufu 2014-06-06
	*
	* @param int $device_id 设备的编号ID
	* @param array $search  搜索条件
	*/
	public function bind_list($device_id, $search=''){
		if(!is_numeric($device_id)){
			return false;
		}
		$sql = "SELECT * FROM {$this->table_bind} WHERE did={$device_id}";
		if($search['state']){
			$sql .= " AND state={$search['state']}";    //1正常2取消
		}
		return $this->_mysql(0)->find($sql);
	}

	/**
	 * 获得绑定信息
	 * @param unknown $id
	 */
	public function bindinfo($id,$cols='*'){
		$sql = "SELECT {$cols} FROM {$this->table_bind} WHERE id={$id}";
		return $this->_mysql(0)->find($sql);
	}


	/**
	 * 查询机手和司机信息
	 * @param array $map  司机、机手、地块 数组
	 * @param int   $pid  项目经理账号id
	 */
	public function getUserinfo($map,$uid){
		$cols="uid,code,comment name,phone";
		//司机
		$sjno=$map['sjno'];
		if($sjno){
			$sql = "SELECT {$cols} FROM {$this->table_set} WHERE type=5 AND uid={$uid} AND code='{$sjno}'";
			$res=$this->_mysql(0)->findone($sql);
			$rtn['sj']=$res?$res:false;
			return $rtn;
		}

		//机手
		$jsno=$map['jsno'];
		if($jsno){
			$sql = "SELECT {$cols} FROM {$this->table_set} WHERE type=3 AND uid={$uid} AND code='{$jsno}'";
			$res=$this->_mysql(0)->findone($sql);
			$rtn['js']=$res?$res:false;
		}

		//地块
		$dkno=$map['dkno'];
		if($dkno){
			$sql = "SELECT {$cols} FROM {$this->table_set} WHERE type=4 AND uid={$uid} AND code='{$dkno}'";
			$res=$this->_mysql(0)->findone($sql);
			$rtn['dk']=$res?$res:false;
		}

		return $rtn;
	}


	/**
	* 获取设备列表数据
	* @author xiezhihui 2014-6-10 09:49:38
	*
	* @param mixed $data
	* @param mixed $page
	* @param mixed $pagesie
	* @return mysql对象
	*/
	public function get_device_list($data,$page=1,$pagesie=10){
		$sql = "select a.* from {$this->table_device} as a where 1";
		if($data['title']){
			$sql.=" And (a.no like '%{$data['title']}%')";
		}
		if($data['state']>0){
			$sql.=" And a.state = '{$data['state']}'";
		}

		if($data['manage']){
			$sql .= " AND (a.master={$data['manage']} OR a.master=0) ";
		}

		if($data['master']){
			$sql .=" AND a.master={$data['master']}";
		}

		if($data['own']>-1){
			$sql .=" AND a.own={$data['own']}";
		}

		$order=$data['order'];
		if($order){
			switch ($order){
				case 'time': $sql.=" ORDER by a.id desc";break;
				case 'update': $sql.=" ORDER by a.update desc";break;
				default:$sql .= " order by a.state desc,a.id desc";
			}
		}

		return $this->_mysql()->findlimit($sql,$page,$pagesie);
	}
	/**
	* 获取一条设备号信息
	* @author xiezhihui 2014-6-10 10:02:23
	* @param mixed $id
	* @return mysql对象
	*/
	public function get_device_info($id){
		if(!$id) return false;
		$sql="select * from {$this->table_device} as a where a.id={$id}";
		return $this->_mysql()->findone($sql);
	}


	/**
	 * 获取一条登记记录详情
	 * @param unknown $id
	 * @return boolean
	 */
	public function get_record_info($id){
		if(!$id) return false;
		$sql="select * from {$this->table_checkin} where id={$id}";
		return $this->_mysql(0)->findone($sql);
	}


	/**
	* 根据条件查询作业点设备的登记列表
	*
	* @param mixed $data
	* @param int $page
	* @param int $pagesize
	*/
	public function get_checkin_list($data,$page=1,$pagesize=10,$cols='*'){
		if((!is_numeric($page) || !is_numeric($pagesize)) && !$pagesize===false){
			return false;
		}
		/***  屏蔽联表查询    @author leiqianyong 2015-10-12 11:25 AM
		if($data['address']){
			$address=$this->address_info($data['address']);
			list($coord_x,$coord_y)=array($address['coord_x'],$address['coord_y']);
			$dis = ",12756274 * asin(sqrt(pow(sin((RADIANS({$coord_y})-RADIANS(a.coord_y))/2),2)+cos(RADIANS({$coord_y}))*cos(RADIANS(a.coord_y))*pow(sin((RADIANS
				({$coord_x})-RADIANS(a.coord_x))/2),2))) as distances ";

		}

		$sql = "SELECT a.*,b.title,c.no as no,d.car_number {$dis} FROM {$this->table_checkin} as a Left Join {$this->table_project} as b On a.pid=b.id Left Join {$this->table_device} as c On a.did=c.id Left Join {$this->table_bind} as d On (a.did=d.did and d.state=1) WHERE 1";
		//根据工程的id查询
		$sql .= " AND a.pid='{$data['pid']}'";//必须选择项目

		if($data['no']){
			$sql .= " AND c.no like '%{$data['no']}%'";
		}
		if($data['start_time']){
			$sql .= " AND a.ctime>=".($data['start_time']*1000);
		}
		if($data['end_time']){
			$sql .= " AND a.ctime<".($data['end_time']*1000);
		}

		$sql .= " ORDER BY a.id DESC";
		if($page === false && $pagesize===false){
			return $this->_mysql(0)->find($sql);
		}

		return $this->_mysql(0)->findlimit($sql,$page,$pagesize);
		**/


		$sql = "SELECT {$cols} FROM {$this->table_checkin} WHERE " ;

		//状态
		$sql.= " state={$data['state']} " ;

		//根据会员id查询记录，总管理查看全部记录
	    $admin_obj = new admin();
		$info = $admin_obj->out_cookie();
		if(empty($info)){return false;}
		if(6!=$info['group']){
			$sql .= " AND (xid={$info['id']} OR xid={$info['master']})";
		}

		if(is_numeric($data['img_del'])){
			$sql .= " AND img_del={$data['img_del']}" ;
		}

		//根据工程的id查询
		if($data['pid']){
		$sql .= " AND pid='{$data['pid']}'";//必须选择项目
		}

		//根据设备号
		if($data['no']){
			$sql .= " AND dno like '%{$data['no']}%'";
		}

		//车牌号
		if($data['dname']){
			$sql .= " AND dname like '%{$data['dname']}%'";
		}

		//司机编号
		if($data['sjno']){
			$sql .= " AND sjno like '%{$data['sjno']}%'";
		}

		//机手编号
		if($data['jsno']){
			$sql .= " AND jsno like '%{$data['jsno']}%'";
		}

		//地块编号
		if($data['dkno']){
			$sql .= " AND dkno like '%{$data['dkno']}%'";
		}

		//挖机模式
		if($data['type']){
			$sql .= " AND type={$data['type']} ";
		}

		//渣车类型
		if($data['truck']){
			$sql .= " AND truck='{$data['truck']}' ";
		}

		//渣车类型
		if($data['onload']){
			$sql .= " AND onload={$data['onload']} ";
		}


		//不饱和模式
		if($data['gcat']>-1){
			$sql .= " AND gcat={$data['gcat']}";
		}


		//土石类型
		if($data['dust']){
			$sql .= " AND dust={$data['dust']} ";
		}

		//时间  是拍照的时间
		if($data['start_time']){
			$sql .= " AND ctime>=".(strtotime($data['start_time']));
		}
		if($data['end_time']){
			$sql .= " AND ctime<".(strtotime($data['end_time']));
		}

		//分班时段查询
        if($data['fbtime']){
            $fbrtn=explode("-",$data['fbtime']);
            $parseconf=(intval(str_replace(':','',$fbrtn[0]))>intval(str_replace(':','',$fbrtn[1])))?'OR':'AND'; //解决跨天的情况，如果开始时间大于截止时间则使用 或
            $sql .=" AND (FROM_UNIXTIME(`ctime`,'%H:%i:%s')>='".$fbrtn[0]."' ".$parseconf." FROM_UNIXTIME(`ctime`,'%H:%i:%s')<='".$fbrtn[1]."')";
        }

		//工作点
		if($data['posi']){
			$sql .= " AND posi={$data['posi']}";
		}

		//线路
		if($data['line']){
			$sql .=" AND line LIKE '%{$data['line']}%'";
		}

		//挖机
		if($data['car']){
			$sql .=" AND wj_no LIKE '%{$data['car']}%'";
		}

		//排序方式  1-车辆排序  2-路线排序
		$sortway=$data['sortway']?$data['sortway']:1;
		if(1==$sortway){
		   $sql .= " ORDER BY dname ASC,ctime DESC";
		}else{
		   $sql .= " ORDER BY line ASC,ctime DESC";
		}

		if($page === false && $pagesize===false){
			return $this->_mysql(0)->find($sql);
		}

		return $this->_mysql(0)->findlimit($sql,$page,$pagesize);
	}


	/**
	 * 根据条件查询作业点设备的登记列表
	 *
	 * @param mixed $data
	 * @param int $page
	 * @param int $pagesize
	 */
	public function get_checkin_list_all($data,$cols='*'){

		$sql = "SELECT {$cols} FROM {$this->table_checkin} WHERE " ;

		//状态
		$sql.= " state={$data['state']} " ;

		//根据会员id查询记录，总管理查看全部记录
		$admin_obj = new admin();
		$info = $admin_obj->out_cookie();
		if(empty($info)){return false;}
		if(6!=$info['group']){
			$sql .= " AND (xid={$info['id']} OR xid={$info['master']})";
		}

		if(is_numeric($data['img_del'])){
			$sql .= " AND img_del={$data['img_del']}" ;
		}

		//根据工程的id查询
		if($data['pid']){
			$sql .= " AND pid='{$data['pid']}'";//必须选择项目
		}

		//根据设备号
		if($data['no']){
			$sql .= " AND dno like '%{$data['no']}%'";
		}

		//车牌号
		if($data['dname']){
			$sql .= " AND dname like '%{$data['dname']}%'";
		}

		//挖机模式
		if($data['type']){
			$sql .= " AND type={$data['type']} ";
		}

		//渣车类型
		if($data['truck']){
			$sql .= " AND truck='{$data['truck']}' ";
		}

		//渣车类型
		if($data['onload']){
			$sql .= " AND onload={$data['onload']} ";
		}

		//不饱和模式
		if($data['gcat']>-1){
			$sql .= " AND gcat={$data['gcat']}";
		}

		//土石类型
		if($data['dust']){
			$sql .= " AND dust={$data['dust']} ";
		}

		//时间  是拍照的时间
		if($data['start_time']){
			$sql .= " AND ctime>=".(strtotime($data['start_time']));
		}
		if($data['end_time']){
			$sql .= " AND ctime<".(strtotime($data['end_time']));
		}

		//工作点
		if($data['posi']){
			$sql .= " AND posi={$data['posi']}";
		}

		//线路
		if($data['line']){
			$sql .=" AND line LIKE '%{$data['line']}%'";
		}

		//挖机
		if($data['car']){
			$sql .=" AND wj_no LIKE '%{$data['car']}%'";
		}

		return $this->_mysql(0)->find($sql);
	}


	/**
	 * 根据条件查询作业点设备的登记列表
	 *
	 * @param mixed $data
	 * @param int $page
	 * @param int $pagesize
	 */
	public function search_checkin_list($data,$cols='*'){
		$sql = "SELECT {$cols} FROM {$this->table_checkin} WHERE 1 " ;

		//状态
		$sql.= " AND state={$data['state']} " ;

		//根据会员id查询记录，总管理查看全部记录
		$admin_obj = new admin();
		$info = $admin_obj->out_cookie();
		if(empty($info)){return false;}
		if(6!=$info['group']){
			$sql .= " AND xid={$info['id']} ";
		}

		//根据工程的id查询
		if($data['pid']){
			$sql .= " AND pid='{$data['pid']}'";//必须选择项目
		}

		//根据设备号
		if($data['no']){
			$sql .= " AND dno like '%{$data['no']}%'";
		}

		//车牌号
		if($data['dname']){
			$sql .= " AND dname like '%{$data['dname']}%'";
		}

		//挖机模式
		if($data['type']){
			$sql .= " AND type={$data['type']} ";
		}

		//渣车类型
		if($data['truck']){
			$sql .= " AND truck='{$data['truck']}' ";
		}

		//渣车类型
		if($data['onload']){
			$sql .= " AND onload={$data['onload']} ";
		}

		//土石类型
		if($data['dust']){
			$sql .= " AND dust={$data['dust']} ";
		}

		//时间  是拍照的时间
		if($data['start_time']){
			$sql .= " AND ctime>=".(strtotime($data['start_time']));
		}
		if($data['end_time']){
			$sql .= " AND ctime<".(strtotime($data['end_time']));
		}

		$sql .= " ORDER BY did ASC,ctime DESC";

		return $this->_mysql(0)->find($sql);
	}


	/**
	 * 根据条件查询作业点设备的登记列表
	 *
	 * @param mixed $data
	 * @param int $page
	 * @param int $pagesize
	 */
	public function get_record_list($data,$page=1,$pagesize=10){
		if((!is_numeric($page) || !is_numeric($pagesize)) && !$pagesize===false){
			return false;
		}

		$sql = "SELECT * FROM {$this->table_checkin} WHERE state=1" ;

		//根据会员id查询记录，总管理查看全部记录
		$admin_obj = new admin();
		$info = $admin_obj->out_cookie();
		if(empty($info)){return false;}
		if(6!=$info['group']){
			$sql .= " AND xid={$info['id']} ";
		}

		//根据工程的id查询
		if($data['pid']){
			$sql .= " AND pid='{$data['pid']}'";//必须选择项目
		}


		$sql .= " ORDER BY id DESC";
		if($page === false && $pagesize===false){
			return $this->_mysql(0)->find($sql);
		}

		return $this->_mysql(0)->findlimit($sql,$page,$pagesize);
	}


	/**
	 * 根据条件查询计时的登记列表
	 *
	 * @param mixed $data
	 * @param int $page
	 * @param int $pagesize
	 */
	public function get_time_list($data,$page=1,$pagesize=10){
		if((!is_numeric($page) || !is_numeric($pagesize)) && !$pagesize===false){
			return false;
		}
		$sql = "SELECT id,pid,dname,downer,ctime,time,dno,img,delimg,count(id) AS ti FROM {$this->table_checkin} WHERE type=2 " ;


		//状态
		$sql.= " AND state={$data['state']} " ;

		//根据会员id查询记录，总管理查看全部记录
		$admin_obj = new admin();
		$info = $admin_obj->out_cookie();
		if(empty($info)){return false;}
		if(6!=$info['group']){
			$sql .= " AND (xid={$info['id']} OR xid={$info['master']})";
		}

		//根据工程的id查询
		if($data['pid']){
			$sql .= " AND pid='{$data['pid']}'";//必须选择项目
		}

		//根据设备号
		if($data['no']){
			$sql .= " AND dno like '%{$data['no']}%'";
		}

		//车牌号
		if($data['dname']){
			$sql .= " AND dname like '%{$data['dname']}%'";
		}

		//挖机模式
		if($data['type']){
			$sql .= " AND type={$data['type']} ";
		}

		//渣车类型
		if($data['truck']){
			$sql .= " AND truck='{$data['truck']}' ";
		}

		//渣车类型
		if(1==$data['type'] && $data['onload']){
			$sql .= " AND onload='{$data['onload']}' ";
		}

		//土石类型
		if($data['dust']){
			$sql .= " AND dust={$data['dust']} ";
		}

		if($data['start_time']){
			$sql .= " AND ctime>=".(strtotime($data['start_time']));
		}
		if($data['end_time']){
			$sql .= " AND ctime<".(strtotime($data['end_time']));
		}

        //分班时段查询
        if($data['fbtime']){
            $fbrtn=explode("-",$data['fbtime']);
            $parseconf=(intval(str_replace(':','',$fbrtn[0]))>intval(str_replace(':','',$fbrtn[1])))?'OR':'AND'; //解决跨天的情况，如果开始时间大于截止时间则使用 或
            $sql .=" AND (FROM_UNIXTIME(`ctime`,'%H:%i:%s')>='".$fbrtn[0]."' ".$parseconf." FROM_UNIXTIME(`ctime`,'%H:%i:%s')<='".$fbrtn[1]."')";
        }

		$sql .= " GROUP BY pid,did,`group` ORDER BY ctime DESC";
		if($page === false && $pagesize===false){
			return $this->_mysql(0)->find($sql);
		}
		//if(1==$info['id'])
		return $this->_mysql(0)->findlimit($sql,$page,$pagesize,'group');
	}



	/**
	 * 根据条件查询计时的登记列表
	 *
	 * @param mixed $data
	 * @param int $page
	 * @param int $pagesize
	 */
	public function get_time_list_all($data,$page=1,$pagesize=10){
		if((!is_numeric($page) || !is_numeric($pagesize)) && !$pagesize===false)return false;

		$sql = "SELECT id,pid,dname,downer,ctime,time,dno,img,delimg,jsno,dkno FROM {$this->table_checkin} WHERE type=2 " ;


		//状态
		$sql.= " AND state={$data['state']} " ;

		//根据会员id查询记录，总管理查看全部记录
		$admin_obj = new admin();
		$info = $admin_obj->out_cookie();
		if(empty($info)){return false;}
		if(6!=$info['group']){
			$sql .= " AND (xid={$info['id']} OR xid={$info['master']})";
		}

		//根据工程的id查询
		if($data['pid']){
			$sql .= " AND pid='{$data['pid']}'";//必须选择项目
		}

		//根据设备号
		if($data['no']){
			$sql .= " AND dno like '%{$data['no']}%'";
		}

		//车牌号
		if($data['dname']){
			$sql .= " AND dname like '%{$data['dname']}%'";
		}

		//挖机模式
		if($data['type']){
			$sql .= " AND type={$data['type']} ";
		}

		//渣车类型
		if($data['truck']){
			$sql .= " AND truck='{$data['truck']}' ";
		}

		//渣车类型
		if(1==$data['type'] && $data['onload']){
			$sql .= " AND onload='{$data['onload']}' ";
		}

		//土石类型
		if($data['dust']){
			$sql .= " AND dust={$data['dust']} ";
		}

		if($data['jsno']){
			$sql .= " AND jsno like '%{$data['jsno']}%' " ;
		}

		if($data['dkno']){
			$sql .= " AND dkno like '%{$data['dkno']}%' " ;
		}

		if($data['start_time']){
			$sql .= " AND ctime>=".(strtotime($data['start_time']));
		}
		if($data['end_time']){
			$sql .= " AND ctime<".(strtotime($data['end_time']));
		}

		//分班时段查询
		if($data['fbtime']){
			$fbrtn=explode("-",$data['fbtime']);
			$parseconf=(intval(str_replace(':','',$fbrtn[0]))>intval(str_replace(':','',$fbrtn[1])))?'OR':'AND'; //解决跨天的情况，如果开始时间大于截止时间则使用 或
			$sql .=" AND (FROM_UNIXTIME(`ctime`,'%H:%i:%s')>='".$fbrtn[0]."' ".$parseconf." FROM_UNIXTIME(`ctime`,'%H:%i:%s')<='".$fbrtn[1]."')";
		}

		$sql .= "  ORDER BY did ASC,jsno ASC,dkno ASC,ctime desc";
		if($page === false && $pagesize===false)return $this->_mysql(0)->find($sql);

		return $this->_mysql(0)->findlimit($sql,$page,$pagesize);
	}




	/**
	* 获取登记记录表的最早时间和最大时间
	* @author xiezhihui 2014-7-1 11:09:18
	* @param mixed $id 项目id号
	* @param mixed $order 倒序 或升序 asc desc
	*/
	public function get_chekin_info_pid($id,$order='asc'){
		if(!$id){
			return false;
		}
		$sql = "select id,ctime,time from {$this->table_checkin} where pid=$id";
		$sql .= " order by id $order limit 1";

		return $this->_mysql(0)->find($sql);
	}
	/**
	* 获取项目绑定设备列表
	*
	* @param mixed $data
	* @param mixed $page
	* @param mixed $pagesize
	*/
	public function get_bind_list($data,$page=1,$pagesize=10){
		if(!is_numeric($page) || !is_numeric($pagesize)){
			return false;
		}
		$sql = "SELECT a.*,b.title,c.no FROM {$this->table_bind} as a,{$this->table_project} as b,{$this->table_device} as c WHERE 1";
		if($data['pid']){
			$sql .= " AND a.pid in {$data['pid']}";
		}
		if($data['state']){
			$sql .= " AND a.stte={$data['state']}";
		}
		$sql .= " ORDER BY time DESC";
		return $this->_mysql(0)->findlimit($sql,$page,$pagesize);
	}


	/**
	 * 获取挖机模式下的数据统计报表
	 * @param array $data 查询条件
	 */
	public function get_report($data){

		$sql = "SELECT did,dname,downer,car_type,dno,truck,dust,jsno,doper,dkno,COUNT(id) AS number FROM {$this->table_checkin} WHERE type=1 AND state=1 " ;

		//设置查询条件
        $sql = $this->set_cond($data,$sql);

		$sql .= " GROUP BY did,truck,dust,jsno,dkno ORDER BY dname asc" ;

		$res = $this->_mysql(0)->find($sql);
		if(empty($res))return false;

		//2018-10-16，解决分组不对数据重复
		/***
		$result=array();
		foreach($res as $key=>$val){

			 if(empty($result[$val['did']])){
				 $result[$val['did']]['did'] = $val['did'] ;
				 $result[$val['did']]['dname'] = $val['dname'] ;
				 $result[$val['did']]['downer'] = $val['downer'] ;
				 $result[$val['did']]['dno'] = $val['dno'] ;
				 $result[$val['did']]['doper'] = $val['doper'] ;
				 $result[$val['did']]['dkno'] = $val['dkno'] ;
				 $result[$val['did']]['jsno'] = $val['jsno'] ;
				 $result[$val['did']][$val['truck'].'_'.$val['dust']] = $val['number'] ;
				 continue;
			 }


			$result[$val['did']][$val['truck'].'_'.$val['dust']] = $val['number'] ;
		}

		return $result;

		//第二种方式
		foreach($res as $key=>$val){
		   //$res[$key][$val['truck'].'_'.$val['dust']]=$val['number'];
		}
        return $res;
		**/

		//@author leiqianyong 2018-10-29 11:58 AM
		$result=array();
		foreach($res as $key=>$val){
			$ak=md5($val['did'].'_'.$val['jsno'].'_'.$val['dkno']);
			if(!$result[$ak])$result[$ak]=$val;
			$result[$ak][$val['truck'].'_'.$val['dust']]=$val['number'];
			$result[$ak]['asum']+=$val['number'];
		}

		return $result;
	}


	/**
	 * 获取挖机模式下的数据记录
	 * @param array $data 查询条件
	 */
	public function get_report_all($data,$type=1){

		$sql = "SELECT * FROM {$this->table_checkin} WHERE type={$type} " ;

		//设置查询条件
		$sql = $this->set_cond($data, $sql);

		$sql .= " ORDER BY dname asc,ctime asc" ;

		$result = $this->_mysql(0)->find($sql);

		return $result ;
	}

	/**
	 * 获取挖机模式下的数据记录
	 * @param array $data 查询条件
	 */
	public function get_report_all_for_report($data,$type=1){

		$sql = "SELECT * FROM {$this->table_checkin} WHERE type={$type} " ;

		//设置查询条件
		$sql = $this->set_cond_for_report($data, $sql);

		$sql .= " ORDER BY dname asc,ctime asc" ;

		$result = $this->_mysql(0)->find($sql);

		return $result ;
	}


	//设置查询条件
	private function set_cond_for_report($data,$sql){

		//是否查询全部信息
		$sql .= " AND state=1 " ;

		//项目id
		if($data['pid']){
			$sql .= " AND pid={$data['pid']} " ;
		}

		//开始时间
		if($data['start_time']){
			$start_time = strtotime($data['start_time']);
			$sql .= " AND ctime>={$start_time}" ;
		}

		//截止时间
		if($data['end_time']){
			$end_time = strtotime($data['end_time']);
			$sql .= " AND ctime<={$end_time}" ;
		}

        //分班时段查询
        if($data['fbtime']){
            $fbrtn=explode("-",$data['fbtime']);
            $parseconf=(intval(str_replace(':','',$fbrtn[0]))>intval(str_replace(':','',$fbrtn[1])))?'OR':'AND'; //解决跨天的情况，如果开始时间大于截止时间则使用 或
            $sql .=" AND (FROM_UNIXTIME(`ctime`,'%H:%i:%s')>='".$fbrtn[0]."' ".$parseconf." FROM_UNIXTIME(`ctime`,'%H:%i:%s')<='".$fbrtn[1]."')";
        }

		return $sql;
	}



	/**
	 * 获取渣车模式下挖机的装车明细
	 * @param array $data 查询条件  @author leiqianyong 2016-3-16 17:23 PM
	 */
	public function get_wj_report($data,$cols='*'){
		$sql = "SELECT {$cols} FROM {$this->table_checkin} WHERE type=3 ";  //AND posi=1

		//设置查询条件
		$sql = $this->set_cond($data, $sql);

		$sql .= " ORDER BY wj_no ASC,dname ASC,time ASC" ;

		$result = $this->_mysql(0)->find($sql);

		return $result ;
	}


	/**
	 * 获取渣车模式下挖机的装车明细
	 * @param array $data 查询条件  @author leiqianyong 2016-3-16 17:23 PM
	 */
	public function get_line_report($data,$cols='*'){
		$sql = "SELECT {$cols} FROM {$this->table_checkin} WHERE type=3 "; //AND posi=2

		//设置查询条件
		$sql = $this->set_cond($data, $sql);

		$sql .= " ORDER BY line ASC,dname ASC,time ASC" ;

		$result = $this->_mysql(0)->find($sql);

		return $result ;
	}


	/**
	 * 获取渣车模式下挖机的装车明细
	 * @param array $data 查询条件  @author leiqianyong 2016-3-16 17:23 PM
	 */
	public function get_car_report($data,$cols='*'){
		$sql = "SELECT {$cols} FROM {$this->table_checkin} WHERE type=3"; //AND posi=2

		//设置查询条件
		$sql = $this->set_cond($data, $sql);

		$sql .= " ORDER BY dname ASC,line ASC,time ASC" ;

		$result = $this->_mysql(0)->find($sql);

		return $result ;
	}



	/**
	 * 获取渣车模式下渣车装车统计表
	 * @param array $data 查询条件
	 * @author leiqianyong 2016-3-17 9:30 AM
	 */
	public function get_car_total_report($data,$cols='*'){
		$sql = "SELECT COUNT(id) AS sum,dname,car_type,line,line_dis,did,sjno,doper FROM {$this->table_checkin} WHERE type=3 ";//AND posi=2

		//设置查询条件
		$sql = $this->set_cond($data, $sql);

		$sql .= " GROUP BY dname,line,sjno" ;

		$result = $this->_mysql(0)->find($sql);

		return $result ;
	}


	/**
	 * 获取渣车模式下渣车装车统计表
	 * @param array $data 查询条件
	 * @author leiqianyong 2016-3-17 9:30 AM
	 */
	public function _get_car_total_report($data,$cols='*'){
		$sql = "SELECT dname,car_type,line,line_dis,did,sjno,doper,ctime,yunno FROM {$this->table_checkin} WHERE type=3 ";//AND posi=2

		//设置查询条件
		$sql = $this->set_cond($data, $sql);

		$sql .= " ORDER BY dname ASC,ctime ASC";

		$result = $this->_mysql(0)->find($sql);

		return $result ;
	}


	public function _get_car_total_report_for_report($data,$cols='*'){
		$sql = "SELECT dname,car_type,line,line_dis,did,sjno,doper,ctime FROM {$this->table_checkin} WHERE type=3 ";//AND posi=2

		//设置查询条件
		$sql = $this->set_cond_for_report($data, $sql);

		$sql .= " ORDER BY dname ASC,ctime ASC";

		$result = $this->_mysql(0)->find($sql);

		return $result ;
	}


	/**
	 * 获取渣车模式下挖机装车统计表
	 * @param array $data 查询条件
	 * @author leiqianyong 2016-3-17 9:30 AM
	 */
	public function get_wj_total_report($data,$cols='*'){
		$sql = "SELECT COUNT(id) AS sum,wj_no,wj_xh,car_type,did FROM {$this->table_checkin} WHERE type=3 ";//AND posi=1

		//设置查询条件
		$sql = $this->set_cond($data, $sql);

		$sql .= " GROUP BY wj_no,car_type" ;

		$result = $this->_mysql(0)->find($sql);

		return $result ;
	}


	//设置查询条件
    private function set_cond($data,$sql){
    	//根据会员id查询记录，总管理查看全部记录
    	$admin_obj = new admin();
    	$info = $admin_obj->out_cookie();
    	if(empty($info)){return false;}
    	if(6!=$info['group']){
    		$sql .= " AND (xid={$info['id']} OR xid={$info['master']})";
    	}

    	//是否查询全部信息
    	if(empty($data['all'])){
    		$sql .= " AND state=1 " ;
    	}

    	//项目id
    	if($data['pid']){
    		$sql .= " AND pid={$data['pid']} " ;
    	}

    	//设备id
    	if($data['no']){
    		$sql .= " AND dno like '%{$data['no']}%' " ;
    	}

    	//车牌号
    	if($data['dname']){
    		$sql .= " AND dname like '%{$data['dname']}%' " ;
    	}

    	//登记状态
        if($data['posi']){
            $sql .= " AND posi={$data['posi']} ";
        }


    	//渣车类型
    	if($data['truck']){
    		$sql .= " AND truck='{$data['truck']}' " ;
    	}

    	//土石类型
    	if($data['dust']){
    		$sql .= " AND dust={$data['dust']} " ;
    	}

    	//装车类型
    	if($data['onload']){
    		$sql .= " AND onload={$data['onload']} " ;
    	}


    	//开始时间
    	if($data['start_time']){
    		$start_time = strtotime($data['start_time']);
    		$sql .= " AND ctime>={$start_time}" ;
    	}

    	//截止时间
    	if($data['end_time']){
    		$end_time = strtotime($data['end_time']);
    		$sql .= " AND ctime<={$end_time}" ;
    	}

        //分班时段查询
        if($data['fbtime']){
            $fbrtn=explode("-",$data['fbtime']);
            $parseconf=(intval(str_replace(':','',$fbrtn[0]))>intval(str_replace(':','',$fbrtn[1])))?'OR':'AND'; //解决跨天的情况，如果开始时间大于截止时间则使用 或
            $sql .=" AND (FROM_UNIXTIME(`ctime`,'%H:%i:%s')>='".$fbrtn[0]."' ".$parseconf." FROM_UNIXTIME(`ctime`,'%H:%i:%s')<='".$fbrtn[1]."')";
        }

    	return $sql;
    }


	/**
	 * 获取计时模式下的数据报表
	 * @param array $data 查询条件
	 * @param type  $type 类型  1-登录查询  2-报表查询
	 */
	public function get_time_report($data,$type=1){

		$sql = "SELECT did,pid,car_type,jsno,dkno,dname,downer,dno,COUNT(id) AS number FROM {$this->table_checkin} WHERE type=2 " ;

		if(1==$type){
		    $sql=$this->set_time_cond($data, $sql);
		}else{
			$sql=$this->set_cond_for_report($data, $sql);
		}

		$sql .= " GROUP BY did,`group`" ;


		$res = $this->_mysql(0)->find($sql);
		if(empty($res)){
			return false;
		}

			$result = array();
		foreach($res as $val){
			$number=($val['number']>1)?($val['number']-1):$val['number'];
			if(empty($result[$val['did']])){
				$result[$val['did']]['did'] = $val['did'] ;
				$result[$val['did']]['dname'] = $val['dname'] ;
				$result[$val['did']]['downer'] = $val['downer'] ;
				$result[$val['did']]['dno'] = $val['dno'] ;
				$result[$val['did']]['car_type'] = $val['car_type'] ;
				$result[$val['did']]['jsno'] = $val['jsno'] ;
				$result[$val['did']]['dkno'] = $val['dkno'] ;
				//$result[$val['did']]['number'] = $val['number'] ;
				//如果1张照片以上，减除第一张  2019-06-20

				$result[$val['did']]['number'] = $number;
				$result[$val['did']]['times'] = 1 ;
				$result[$val['did']]['pid'] = $val['pid'] ;
				continue;
			}

		   	//$result[$val['did']]['number'] = $result[$val['did']]['number']+$val['number'] ;
			//如果1张照片以上，减除第一张  2019-06-20
			$result[$val['did']]['number'] = $result[$val['did']]['number']+$number;
		   	$result[$val['did']]['times'] = $result[$val['did']]['times']+1 ;
		}

		return $result;
	}


	/**
	 * @author leiqianyong 2020-3-23 14:33 PM
	 * 获取计时模式下的数据报表
	 * @param array $data 查询条件
	 */
	public function get_act_time_report($data){

		/****
		$sql = "SELECT did,pid,car_type,jsno,dkno,dname,downer,dno,`group`,COUNT(id) AS number,MIN(ctime) mintime,MAX(ctime) maxtime FROM {$this->table_checkin} WHERE type=2 " ;
		$sql=$this->set_time_cond($data, $sql);
		$sql .= " GROUP BY did,`group` HAVING number>1" ;  //剔除只有一张照片的情况
        ***/

		$sql = "SELECT did,pid,car_type,jsno,dkno,dname,downer,dno,`group`,ctime FROM {$this->table_checkin} WHERE type=2 " ;
		$sql=$this->set_time_cond($data, $sql);
		//$sql .= " ORDER BY did ASC,jsno ASC,dkno ASC,ctime ASC" ;  设备号发生变化，数据不正确 20200508
		$sql .= " ORDER BY dname ASC,jsno ASC,dkno ASC,ctime ASC" ;

		return $this->_mysql(0)->find($sql);
	}



	public function get_act_time_report_parse($data){
		$list=$this->get_act_time_report($data);

		if(!$list)return false;

		//按设备组合数据，用于格式化表格
		$group=0; //设备分组编号
		$tag_dev='';//设备编号
		$group_posi=0;//记录在组中的位置
		$time_start=0; //记录开始时间
		$time_end=0; //记录截止时间
		$group_num=0; //分组中记录数量
		foreach ($list as $key=>$vo){
			//如果设备号发生变化，变更对应参数
			$tagkey=$vo['dname'].'-'.$vo['jsno'].'-'.$vo['dkno'];
			if($tagkey!=$tag_dev){
				$group=0;
				$group_posi=0;
				$group_num=0;
				$tag_dev=$tagkey;
			}

			//分组中第一个记录
			if(0==$group_posi){
				$group_num++;
				$time_end=$time_start=$vo['ctime'];
				$vo['maxtime']=$vo['mintime']=$vo['ctime'];
				$vo['num']=$group_num;
				$group_posi++;
			}else{
				//间隔时间超过5分钟，重新分组
				if(($vo['ctime']-$time_end)>300){
					$group++;
					$group_num=1;
					$group_posi=1;
					$vo['num']=$group_num;
					$time_end=$time_start=$vo['ctime'];
					$vo['maxtime']=$vo['mintime']=$vo['ctime'];
				}else{
					$group_num++;
					$vo['num']=$group_num;
					$time_end=$vo['ctime'];
					$vo['mintime']=$time_start;
					$vo['maxtime']=$vo['ctime'];
					$group_posi++;
				}
			}

			$res[$tag_dev][$group]=$vo;
		}

		if(!$res)return false;

		//剔除一条记录
		foreach ($res as $key=>$vo){
			$rtn=false;
			foreach($vo as $k=>$v)if($v['num']>1)$rtn[]=$v;
			if($rtn)$result[$key]=$rtn;
		}

		if(!$result)return false;

		ksort($result);

		//每个单位总计时间
		foreach($result as $key=>$vo) {
			$time_per_sum = 0; //总时间
			foreach ($vo as $cv) $time_per_sum +=$cv['maxtime'] - $cv['mintime'];

			$single=$vo[0];
            $single['sum_time']=$time_per_sum;
            $tag_key=md5($single['dname'].'_'.$single['jsno'].'_'.$single['dkno']);
			$returnres[$tag_key]=$single;
		}

		return $returnres;
	}



	/**
	 * 获取计时模式下的数据报表
	 * @param array $data 查询条件
	 */
	public function get_time_report_for_report($data){

		$sql = "SELECT did,pid,car_type,jsno,dkno,dname,downer,dno,COUNT(id) AS number FROM {$this->table_checkin} WHERE type=2 " ;

		$sql=$this->set_cond_for_report($data, $sql);

		$sql .= " GROUP BY did,`group`" ;
		$res = $this->_mysql(0)->find($sql);
		if(empty($res)){
			return false;
		}

		$result = array();
		foreach($res as $val){
			$number=($val['number']>1)?($val['number']-1):$val['number'];
			if(empty($result[$val['did']])){
				$result[$val['did']]['did'] = $val['did'] ;
				$result[$val['did']]['dname'] = $val['dname'] ;
				$result[$val['did']]['downer'] = $val['downer'] ;
				$result[$val['did']]['dno'] = $val['dno'] ;
				$result[$val['did']]['car_type'] = $val['car_type'] ;
				$result[$val['did']]['jsno'] = $val['jsno'] ;
				$result[$val['did']]['dkno'] = $val['dkno'] ;
				//$result[$val['did']]['number'] = $val['number'] ;
				//如果1张照片以上，减除第一张  2019-06-20

				$result[$val['did']]['number'] = $number;
				$result[$val['did']]['times'] = 1 ;
				$result[$val['did']]['pid'] = $val['pid'] ;
				continue;
			}

			//$result[$val['did']]['number'] = $result[$val['did']]['number']+$val['number'] ;
			//如果1张照片以上，减除第一张  2019-06-20
			$result[$val['did']]['number'] = $result[$val['did']]['number']+$number;
			$result[$val['did']]['times'] = $result[$val['did']]['times']+1 ;
		}

		return $result;
	}



	/**
	 * 获取计时模式下的数据报表
	 * @param array $data 查询条件
	 */
	public function get_time_report_all($data){

		$sql = "SELECT * FROM {$this->table_checkin} WHERE type=2 " ;

		$sql=$this->set_time_cond($data, $sql);

		$sql .= " ORDER BY ctime DESC" ;
		$res = $this->_mysql(0)->find($sql);
		if(empty($res)){
			return false;
		}

		return $res;
	}


	private function set_time_cond($data,$sql){
		//根据会员id查询记录，总管理查看全部记录
		$admin_obj = new admin();
		$info = $admin_obj->out_cookie();
		if(empty($info)){return false;}
		if(6!=$info['group']){
			$sql .= " AND (xid={$info['id']} OR xid={$info['master']})";
		}

		//是否查询全部信息
		if(empty($data['all'])){
			$sql .= " AND state=1 " ;
		}

		//项目id
		if($data['pid']){
			$sql .= " AND pid={$data['pid']} " ;
		}

		//设备id
		if($data['no']){
			$sql .= " AND dno like '%{$data['no']}%' " ;
		}

		//车牌号
		if($data['dname']){
			$sql .= " AND dname like '%{$data['dname']}%' " ;
		}

		//机手编号
		if($data['jsno']){
			$sql .= " AND jsno like '%{$data['jsno']}%' " ;
		}

		//地块编号
		if($data['dkno']){
			$sql .= " AND dkno like '%{$data['dkno']}%' " ;
		}

		//开始时间
		if($data['start_time']){
			$start_time = strtotime($data['start_time']);
			$sql .= " AND ctime>={$start_time}" ;
		}


		//截止时间
		if($data['end_time']){
			$end_time = strtotime($data['end_time']);
			$sql .= " AND ctime<={$end_time}" ;
		}

        //分班时段查询
        if($data['fbtime']){
            $fbrtn=explode("-",$data['fbtime']);
            $parseconf=(intval(str_replace(':','',$fbrtn[0]))>intval(str_replace(':','',$fbrtn[1])))?'OR':'AND'; //解决跨天的情况，如果开始时间大于截止时间则使用 或
            $sql .=" AND (FROM_UNIXTIME(`ctime`,'%H:%i:%s')>='".$fbrtn[0]."' ".$parseconf." FROM_UNIXTIME(`ctime`,'%H:%i:%s')<='".$fbrtn[1]."')";
        }

		return $sql;
	}



	/**
	 * 获取挖机模式下的统计数据报表       统计车数和方量
	 * @param array $data 查询条件
	 */
	public function get_total_report($data){

		$sql = "SELECT COUNT(id) AS number FROM {$this->table_checkin} WHERE type=1 AND state=1 " ;

		//根据会员id查询记录，总管理查看全部记录
		$admin_obj = new admin();
		$info = $admin_obj->out_cookie();
		if(empty($info)){return false;}
		if(6!=$info['group']){
			$sql .= " AND (xid={$info['id']} OR xid={$info['master']}) ";
		}

		//装车类型
		$sql .= " AND onload={$data['onload']} " ;

		//统计时间
		$start_time = strtotime($data['date']);
		$end_time = $start_time + 86400;
		$sql .= " AND ctime>={$start_time} AND ctime<{$end_time}" ;


	//	$sql .= " GROUP BY truck,dust" ;

		$res = $this->_mysql(0)->findone($sql);
		if(empty($res)){
			return false;
		}

		return $res;

		//方量单位
		$_a1=18;
		$_a2=30;
		$_a3=28;
		$_b1=15;
		$_b2=30;
		$_b3=27;
		$_c1=18;
		$_c2=35;
		$_c3=30;

		$result = array();
		foreach($res as $val){
			$result['number'] += $val['number'];
			$xx='_'.$val['truck'].$val['dust'] ;
            $result['total'] += $$xx * $val['number'];
		}


		return $result ;
	}


	/**
	 * 获取挖机模式下的统计数据报表       次数和时间
	 * @param array $data 查询条件
	 */
	public function get_total_time_report($data){

		//$sql = "SELECT truck,dust,COUNT(id) AS number FROM {$this->table_checkin} WHERE type=2 AND state=1 " ;
		$sql = "SELECT COUNT(id) AS number FROM {$this->table_checkin} WHERE type=2 AND state=1 " ;

		//根据会员id查询记录，总管理查看全部记录
		$admin_obj = new admin();
		$info = $admin_obj->out_cookie();
		if(empty($info)){return false;}
		if(6!=$info['group']){
			$sql .= " AND (xid={$info['id']} OR xid={$info['master']}) ";
		}

		//统计时间
		$start_time = strtotime($data['date']);
		$end_time = $start_time + 86400;
		$sql .= " AND ctime>={$start_time} AND ctime<{$end_time}" ;

		$sql .= " GROUP BY did,`group`" ;

		$res = $this->_mysql(0)->find($sql);
		if(empty($res)){
			return false;
		}

		$_num=0;
		$_total=0;
		foreach($res as $val){
		   	$_total += $val['number'] ;
		   	$_num++;
		}

		$result['num']=$_num;
		$result['total']=$_total;

		return $result;
	}



	/**
	 * 获取渣车模式下的统计数据报表       次数和时间
	 * @param array $data 查询条件
	 */
	public function get_car_time_report($data){

		$sql = "SELECT COUNT(id) AS number FROM {$this->table_checkin} WHERE type=3 AND state=1 " ;

		//根据会员id查询记录，总管理查看全部记录
		$admin_obj = new admin();
		$info = $admin_obj->out_cookie();
		if(empty($info)){return false;}
		if(6!=$info['group']){
			$sql .= " AND (xid={$info['id']} OR xid={$info['master']}) ";
		}

		//统计时间
		$start_time = strtotime($data['date']);
		$end_time = $start_time + 86400;
		$sql .= " AND ctime>={$start_time} AND ctime<{$end_time}" ;

		$sql .= " AND posi=1";

		$res = $this->_mysql(0)->findone($sql);
		if(empty($res)){
			return false;
		}

		return $res['number'];
	}




	function get_setinfo($id,$cols='*'){
		if(!is_numeric($id))return false;

		$sql="SELECT {$cols} FROM {$this->table_set} WHERE id={$id}";

		return $this->_mysql(0)->findone($sql);
	}


	function setinfo_add($data){
		return $this->set_db($this->table_set, $data);
	}

	function setinfo_edit($id,$data){
		$where="id={$id}";
		return $this->set_db($this->table_set, $data,false,$where);
	}

	//获取设置信息
	function setinfo_search($pid,$type=1){
		$sql="SELECT * FROM {$this->table_set} WHERE uid={$pid} AND type={$type} ORDER BY state DESC,sort DESC,id DESC";
		return $this->_mysql(0)->find($sql);
	}

	function setinfo_get($uid,$type=0){
		$sql="SELECT * FROM {$this->table_set} WHERE uid={$uid} AND state=1 ";

		if($type && is_numeric($type)){
			$sql.=" AND type={$type} ";
		}elseif($type && is_array($type)){
			$typestr=implode(',', $type);
			$sql.=" AND  type IN (".$typestr.") ";
		}

		$sql.="ORDER BY type ASC,sort DESC,id DESC";

		return $this->_mysql(0)->find($sql);
	}

	function setinfo_del($id,$uid){
		if($uid>1){
			$where=" AND uid={$uid}";
		}
		$sql="DELETE FROM {$this->table_set} WHERE id={$id} ".$where;
		return $this->_mysql(0)->query($sql,true);
	}



	/**
	 * 判断并处理重复记录- 如果记录非重复则返回true  记录重复则返回false且要删除重复的记录
     * @param int $id   当前记录id
     * @param int $code 记录标记字符串集合-唯一值
     * @param
	 */
	public function device_repeat_del($id,$code){
	return true;
		if(!$id)return true;

		$sql = "SELECT id FROM {$this->table_checkin} WHERE code='{$code}' AND id<{$id}";
		$repeat=$this->_mysql(0)->findone($sql);
		if(empty($repeat))return true;

		$sql="DELETE FROM {$this->table_checkin} WHERE id={$id}";
		$this->_mysql(0)->query($sql);
		return false;
	}


	public function device_insert_bat($sql){
		return $this->_mysql(0)->query($sql);
	}


	//获取线路配置 @author leiqianyong 2021-10-19 13:51:13
	public function get_set_line(){
          $sql = "select * from {$this->table_set} where type = 2 and state=1 and yun_min>=0 and yun_max>0";
          $res=$this->_mysql(0)->find($sql);
          if(!$res)return false;

          foreach ($res as $vo){
              $rtn[$vo['uid']][$vo['code']] = $vo;
		  }

          return $rtn;
	}


	//根据负责人查询线路配置 @author leiqianyong 2021-10-19 14:21:20
	public function get_set_line_byuid($uid){
		if(!$uid)return false;
		$sql = "select * from {$this->table_set} where uid={$uid} and type = 10 and state=1 and yun_min>=0 and yun_max>0";
		$res=$this->_mysql(0)->find($sql);
		if(!$res)return false;
		return $res;
	}


}